﻿--[[
   @brief    顶部导航tab面板
   @author   hqzheng
   @version  1.0
   @date     2017-04-12
--]]

navigation_tab = 
{
    mainframe_dialog_id = 0;
    parent_id = 0;
    home_tab_id = 0;
    selected_flag_id = 0;
    selected_panel_id = 0;
    home_tab_ani_id = 0;

    cur_select_tab_id = 0;

    btn_info = {};

    NAV_HEIGHT = 80;
    MENU_OFFSET_X = 8;

    first_switch = true;

    is_english_ver = false;

    tab_ctn_ptr = nil;
    module_id_2_tab_id = {};

    init = function (parent_id, mainframe_dialog_id)
        navigation_tab.initEnglishVer();

        navigation_tab.mainframe_dialog_id = mainframe_dialog_id;
        navigation_tab.parent_id = parent_id;
        navigation_tab.selected_panel_id = ui_utility.get_ctrl_id_by_path(mainframe_dialog_id, "pnlSelected");
        navigation_tab.selected_flag_id = ui_utility.get_ctrl_id_by_path(mainframe_dialog_id, "pnlSelected\\tabSelected");
        navigation_tab.home_tab_ani_id = ui_utility.get_ctrl_id_by_path(parent_id, "CtnHomeAni");
        local plugin_info_path = "mainframe_navigation_tab\\lua_plugins_info.xml";

        if base_utility.CLIENT_TYPE.OVERSEA_VER == base_utility.get_client_version_type() then
            plugin_info_path = "mainframe_navigation_tab\\lua_plugins_info_overseas.xml";
        end

        if true == base_utility.is_netbar_ver() then
            plugin_info_path = "mainframe_navigation_tab\\lua_plugins_info_netbar.xml";
        end

        local node_count = lua_plugin_mgr.add_module_plugins("navigation_tab", plugin_info_path);
        local ctn_width = node_count * 94;
        ui_utility.setproperty_by_id(navigation_tab.parent_id, "Width", ctn_width);
        ui_utility.setproperty_by_id(navigation_tab.selected_panel_id, "Width", ctn_width);
        local cnt_id = ui_utility.get_ctrl_id_by_path(parent_id, "Container_1");
        ui_utility.setproperty_by_id(cnt_id, "Transparent", "true");
        local scrl_view_id = ui_utility.get_ctrl_id_by_path(cnt_id, "ScrollView_1");
        ui_utility.setproperty_by_id(scrl_view_id, "Width", ctn_width);
        ui_utility.setproperty_by_id(scrl_view_id, "Transparent", "true");
        local inner_ctn_id = ui_utility.get_ctrl_id_by_path(scrl_view_id, "Container_2");
        ui_utility.setproperty_by_id(inner_ctn_id, "Transparent", "true");
        ui_utility.setproperty_by_id(navigation_tab.parent_id, "ColumnCount", node_count);
        navigation_tab.tab_ctn_ptr = ui_utility.get_ctrl_ptr_by_id(navigation_tab.parent_id);
        if nil ~= navigation_tab.tab_ctn_ptr then
            navigation_tab.tab_ctn_ptr:ClearAllItems();
        end

        lua_plugin_mgr.load_plugins("navigation_tab");
        navigation_tab.set_select_flag_visible(false);

        ierd_tgp.tpf_ui.Require("mainframe_navigation_tab\\tab_menu.lua");
        comm_center.reg_service("i_service_switch_tab", navigation_tab.handle_switch_tab_service);
        comm_center.reg_service("i_service_get_home_tab_position", navigation_tab.handle_get_home_tab_position);
        comm_center.reg_service("i_service_show_home_tab_flashing", navigation_tab.handle_show_home_tab_flashing);
        comm_center.reg_service("i_service_hide_home_tab_flashing", navigation_tab.handle_hide_home_tab_flashing);
        comm_center.reg_service("i_svr_refresh_nav_tab_status", navigation_tab.refresh_status);
    end;

    refresh_status = function()
        for module_id, tab_id in pairs(navigation_tab.module_id_2_tab_id) do
            if tab_id ~= nil then
                if tab_id == navigation_tab.cur_select_tab_id then
                    navigation_tab.set_btn_slct_status(tab_id);
                else
                    navigation_tab.set_btn_nor_status(tab_id, false);
                end
            end
        end
    end;

    initEnglishVer = function()
        local tpfui = base_utility.get_component_adapter("ITpf_ui", "ierd_tgp.tpf_ui.GetTPFUI();");
        if tpfui ~= nil then
            navigation_tab.is_english_ver = tpfui:Is_English_ver();
        end;
    end;

    inner_log = function (msg, core_msg)
        base_utility.log(string.format("[navigation_tab] %s", msg), core_msg);
    end;

    show = function (data)
        if nil == data then
           return;
        end
        navigation_tab.init(data.nav_tab_panel_id, data.dialog_id);
        ui_utility.set_visible(navigation_tab.parent_id, true);
    end;

    switch_tab = function (tab_type, is_defual_select)
        local target_tab_id = navigation_tab.module_id_2_tab_id[tab_type];
        if target_tab_id == navigation_tab.cur_select_tab_id then
            return;
        end

        navigation_tab.set_btn_slct_status(target_tab_id);
        navigation_tab.set_btn_nor_status(navigation_tab.cur_select_tab_id, true);
        navigation_tab.cur_select_tab_id = target_tab_id;

        -- navigation_tab.move_select_flag(navigation_tab.cur_select_tab_id, false);

        if nil == is_defual_select or false == is_defual_select then
            -- tab切换广播
            local info = {nav_tab_type = tab_type}
            local tab_info = navigation_tab.btn_info[target_tab_id];
            if nil ~= tab_info then
                info.moudle_id = tab_info.module_id;
            end
            comm_center.send_broadcast("i_broadcast_nav_tab_switch", info);
        end
    end;

    set_btn_nor_status = function (btn_id, is_need_ani)
        local btn_info = navigation_tab.btn_info[btn_id];
        if nil == btn_info then
            return;
        end

        local tab_txt_id = ui_utility.get_ctrl_id_by_path(btn_id, "lblText");
        local inner_btn_id = ui_utility.get_ctrl_id_by_path(btn_id, "btnIcon");
        local ani_ctn_id = ui_utility.get_ctrl_id_by_path(btn_id, "AniCtn");

        ui_utility.setproperty_by_id(inner_btn_id, "Background", "NONE");
        if navigation_tab.is_english_ver then
            ui_utility.setproperty_by_id(ani_ctn_id, "Background", btn_info.normal_en);
        else
            if true == is_need_ani then
                ui_utility.setproperty_by_id(ani_ctn_id, "Background", btn_info.un_slct_ani);
            else
                ui_utility.setproperty_by_id(ani_ctn_id, "Background", btn_info.nor_bkg);
            end
        end;
    end;

    set_btn_slct_status = function (btn_id)
        local btn_info = navigation_tab.btn_info[btn_id];
        if nil == btn_info then
            return;
        end

        local tab_txt_id = ui_utility.get_ctrl_id_by_path(btn_id, "lblText");
        local inner_btn_id = ui_utility.get_ctrl_id_by_path(btn_id, "btnIcon");
        local ani_ctn_id = ui_utility.get_ctrl_id_by_path(btn_id, "AniCtn");

        ui_utility.setproperty_by_id(inner_btn_id, "Background", "NONE");
        if navigation_tab.is_english_ver then
            ui_utility.setproperty_by_id(ani_ctn_id, "Background", btn_info.press_en);
        else
            ui_utility.setproperty_by_id(ani_ctn_id, "Background", btn_info.slct_ani);
        end;
    end;

    cal_tab_offset = function (tab_id)
        local info = navigation_tab.btn_info[tab_id];
        if nil == info then
            return 0, 0; 
        end
        local offset_x = tonumber(info.tab_offset_x);
        local offset_y = tonumber(info.tab_offset_y);
        if nil == offset_x then
            offset_x = 0;
        end
        if nil == offset_y then
            offset_y = 0;
        end
        return offset_x, offset_y;
    end;

    move_select_flag = function (dst_tab_id, is_use_animation)
        local select_flag_ptr = ui_utility.get_ctrl_ptr_by_id(navigation_tab.selected_flag_id);
        if nil == select_flag_ptr then
            return;
        end

        navigation_tab.set_select_flag_visible(true);
        local select_flag_rc = select_flag_ptr:GetRectEx(TRUE,TRUE);
       
        local select_flag_w = select_flag_rc.right - select_flag_rc.left;
        local select_flag_h = select_flag_rc.bottom - select_flag_rc.top;
        local cur_tab_offset_x, cur_tab_offset_y = navigation_tab.cal_tab_offset(navigation_tab.cur_select_tab_id);
        local dst_tab_offset_x, dst_tab_offset_y = navigation_tab.cal_tab_offset(dst_tab_id);

        if true == is_use_animation then
            -- 位移动画
            local start_pos = POINT:new_local();
            start_pos.x = cur_tab_offset_x;
            start_pos.y = cur_tab_offset_y;

            local end_pos = POINT:new_local();
            end_pos.x = dst_tab_offset_x;
            end_pos.y = dst_tab_offset_y;

            animation_utility.init(
                navigation_tab.selected_flag_id,
                "Select_Flag_Move", animation_utility.ANI_TYPE.ANI_MOVE);
            animation_utility.move_animation(
                navigation_tab.selected_flag_id, start_pos, end_pos, 8, 0);
        else
            select_flag_rc.left = dst_tab_offset_x;
            select_flag_rc.right = select_flag_rc.left + select_flag_w;
            select_flag_rc.top = dst_tab_offset_y;
            select_flag_rc.bottom = select_flag_rc.top + select_flag_h;
            select_flag_ptr:MoveEx(select_flag_rc,TRUE);
        end
    end;

    on_click_tab = function (uilib, control_id)
        local tab_id = ui_utility.get_ctrl_parent_id(control_id);
        local btn_info = navigation_tab.btn_info[tab_id];
        if nil ~= btn_info then
            base_utility.mainframe_report("main_window", "mainframe_btn_click", btn_info.plugin_name);
            -- navigation_tab.move_select_flag(tab_id, true);
            if btn_info.module_id == GAME_ID.GAEM_ID_HOME_PAGE then
                comm_center.call_service("SrvJumpHomePageModule", {});
                -- ui_utility.setproperty_by_id(navigation_tab.home_tab_ani_id, "Background", "NONE");
            else
                navigation_tab.open_tab_page(tonumber(btn_info.module_id));
            end
            navigation_tab.send_click_event(btn_info.plugin_name);
        end
    end;

    on_tab_mouse_hover = function (uilib, control_id)
        base_utility.log("on_tab_mouse_hover");
        local btn_tab_id = ui_utility.get_ctrl_parent_id(control_id);
        if navigation_tab.cur_select_tab_id ~= btn_tab_id then
            local inner_btn_id = ui_utility.get_ctrl_id_by_path(btn_tab_id, "btnIcon");
            local ani_ctn_id = ui_utility.get_ctrl_id_by_path(btn_tab_id, "AniCtn");
            local btn_info = navigation_tab.btn_info[btn_tab_id];
            ui_utility.setproperty_by_id(ani_ctn_id, "Background", "NONE");
            if navigation_tab.is_english_ver then
                ui_utility.setproperty_by_id(inner_btn_id, "Background", btn_info.hover_en);
            else
                ui_utility.setproperty_by_id(inner_btn_id, "Background", btn_info.nor_hover_ani);
            end;
        end

        -- 显示菜单
        local btn_info = navigation_tab.btn_info[btn_tab_id];
        if nil ~= btn_info and nil ~= btn_info.module_id then
            navigation_tab.show_tab_menu(true, btn_tab_id, btn_info.module_id);
        end
    end;

    on_tab_mouse_leave = function (uilib, control_id)
        local btn_tab_id = ui_utility.get_ctrl_parent_id(control_id);
        if navigation_tab.cur_select_tab_id ~= btn_tab_id then
            local btn_tab_id = ui_utility.get_ctrl_parent_id(control_id);
            local inner_btn_id = ui_utility.get_ctrl_id_by_path(btn_tab_id, "btnIcon");
            local ani_ctn_id = ui_utility.get_ctrl_id_by_path(btn_tab_id, "AniCtn");
            local btn_info = navigation_tab.btn_info[btn_tab_id];
            ui_utility.setproperty_by_id(ani_ctn_id, "Background", "NONE");
            if navigation_tab.is_english_ver then
                ui_utility.setproperty_by_id(inner_btn_id, "Background", btn_info.normal_en);
            else
                ui_utility.setproperty_by_id(inner_btn_id, "Background", btn_info.nor_bkg);
                ui_utility.setproperty_by_id(inner_btn_id, "Background", btn_info.nor_leave_ani);
            end;
        end
        local tpfui = base_utility.get_component_adapter("ITpf_ui",
                        "ierd_tgp.tpf_ui.GetTPFUI();");
        if nil ~= tpfui then
            tpfui:RemoveInterval("navigation_tab.delay_handle_tab_leave_msg", btn_tab_id);
            tpfui:AddInterval("navigation_tab.delay_handle_tab_leave_msg", 200, btn_tab_id);
            base_utility.log("[navigation_tab]on_tab_mouse_leave");
        end
    end; 

    delay_handle_tab_leave_msg = function (btn_tab_id)
        if true == navigation_tab.is_mouse_in_menu_and_btn(btn_tab_id) then
            return;
        end

        local tpfui = base_utility.get_component_adapter("ITpf_ui",
                        "ierd_tgp.tpf_ui.GetTPFUI();");
        if nil ~= tpfui then
            tpfui:RemoveInterval("navigation_tab.delay_handle_tab_leave_msg", btn_tab_id);
        end
        -- 隐藏菜单
        local btn_info = navigation_tab.btn_info[btn_tab_id];
        if nil ~= btn_info and nil ~= btn_info.module_id then
            navigation_tab.show_tab_menu(false, btn_tab_id, btn_info.module_id);
        end
    end;

    set_select_flag_visible = function (is_visible)
        ui_utility.set_visible(navigation_tab.selected_flag_id, is_visible);
    end;

    handle_switch_tab_service = function (data)
        if nil == data or nil == data.tab_type then
            return;
        end
        navigation_tab.switch_tab(data.tab_type);
    end;

    is_mouse_in_menu_and_btn = function (tab_id)
        local btn_info = navigation_tab.btn_info[tab_id];
        if nil == btn_info or nil == btn_info.menu_path then
            return;
        end
        local menu_id = ui_utility.get_ctrl_id_by_path(navigation_tab.mainframe_dialog_id, btn_info.menu_path);
        local menu_ptr = ui_utility.get_ctrl_ptr_by_id(menu_id);
        local inner_btn_ptr = ui_utility.get_ctrl_ptr_by_id(tab_id);
        if nil == menu_ptr or nil == inner_btn_ptr then
            return false;
        end

        local rect_menu = menu_ptr:GetRectEx(TRUE,TRUE); 
        local point = ierd_tgp.tray_ui.Point:new_local();
        local tray_ui =
            base_utility.get_component_adapter("ITray_ui",
                "ierd_tgp.tray_ui.get_Tray_ui_adapter();");
        if tray_ui ~= nil then
            tray_ui:get_cursor_pos(point);
        end             
        local mouse_in = false;
        if rect_menu.left <= point.x and point.x <= rect_menu.right and
            rect_menu.top <= point.y and point.y <= rect_menu.bottom then
            mouse_in = true;
        end

        if not mouse_in then
            -- 判断是否在btn里
            local rect_btn = inner_btn_ptr:GetRectEx(TRUE,TRUE);
            if rect_btn.left <= point.x and point.x <= rect_btn.right and
                rect_btn.top <= point.y and point.y <= rect_btn.bottom then
                mouse_in = true;
            end
        end
        return mouse_in;
    end;

    open_tab_page = function (game_id)
        local param = {};
        local game_lib =
            base_utility.get_component_adapter("IGame_library",
                "ierd_tgp.game_library.Get_game_library_adapter();");
        if nil == game_lib then
            return;
        end

        local channel_id = game_lib:get_bind_channel_id();
        local is_netbar = (channel_id == 1);
        if game_id == GAME_ID.GAEM_ID_HOME_PAGE and is_netbar then
            param.type = "mmo_game";
            param.sub_page = "NETBAR";
            web_utility.open_link(base_utility.table_to_json(param));
        else
            param.module_id = game_id;
            event_center.send_event(banner_event.JUMP_MODULE_WEB, base_utility.table_to_json(param));
        end
    end;

    show_tab_menu = function (is_show, tab_id, game_id)
        local setting_info = wegame_mainframe_model.navigate_tab_menu_setting[tostring(game_id)];
        if nil == setting_info or 0 == tonumber(setting_info.is_show_menu)
            or nil == setting_info.menu_item_info_list
            or "" == setting_info.menu_item_info_list then
            return;
        end

        local menu_id = 0;
        if GAME_ID.GAME_ID_SHOP == game_id then
            menu_id = ui_utility.get_ctrl_id_by_path(navigation_tab.mainframe_dialog_id, "mnShop");
        elseif GAME_ID.GAEM_ID_HOME_PAGE == game_id then
            menu_id = ui_utility.get_ctrl_id_by_path(navigation_tab.mainframe_dialog_id, "mnHome");
        elseif GAME_ID.GAME_ID_GAME_LIVE == game_id then
            menu_id = ui_utility.get_ctrl_id_by_path(navigation_tab.mainframe_dialog_id, "mnVideo");
        elseif GAME_ID.GAME_ID_CHINATOWN == game_id then
            menu_id = ui_utility.get_ctrl_id_by_path(navigation_tab.mainframe_dialog_id, "mnChinaTown");
        end
        
        local btn_tab_ptr = ui_utility.get_ctrl_ptr_by_id(tab_id);
        local mainframe_ptr = ui_utility.get_ctrl_ptr_by_id(navigation_tab.mainframe_dialog_id);
        if nil == btn_tab_ptr or nil == mainframe_ptr then
            return;
        end
        local btn_tab_rc = btn_tab_ptr:GetRectEx(TRUE,TRUE);
        local mainframe_rc = mainframe_ptr:GetRectEx(TRUE,TRUE);
        local offset_x = btn_tab_rc.left - mainframe_rc.left - navigation_tab.MENU_OFFSET_X;
        local offset_y = btn_tab_rc.top - mainframe_rc.top + navigation_tab.NAV_HEIGHT;
        tab_menu.init(menu_id, base_utility.json_to_table(setting_info.menu_item_info_list), game_id);
        tab_menu.show(menu_id, is_show, offset_x, offset_y);
    end;

    handle_get_home_tab_position = function ()
        local btn_tab_ptr = ui_utility.get_ctrl_ptr_by_id(navigation_tab.home_tab_id);
        local mainframe_ptr = ui_utility.get_ctrl_ptr_by_id(navigation_tab.mainframe_dialog_id);
        if nil == btn_tab_ptr or nil == mainframe_ptr then
            return;
        end
        local btn_tab_rc = btn_tab_ptr:GetRectEx(TRUE,TRUE);
        local mainframe_rc = mainframe_ptr:GetRectEx(TRUE,TRUE);
        local btn_tab_h = btn_tab_rc.bottom - btn_tab_rc.top;
        local btn_tab_w = btn_tab_rc.right - btn_tab_rc.left;

        local position_info = {};
        position_info["mainframe_top"] = mainframe_rc.top;
        position_info["mainframe_left"] = mainframe_rc.left;
        position_info["home_tab_center_x"] = btn_tab_rc.left - mainframe_rc.left + btn_tab_w / 2;
        position_info["home_tab_center_y"] = btn_tab_rc.top - mainframe_rc.top + btn_tab_h / 2;
        base_utility.log(string.format("[handle_get_home_tab_position]mainframe_top:%s, mainframe_left:%s, home_tab_center_x:%s, home_tab_center_y:%s",
            position_info.mainframe_top, position_info.mainframe_left, position_info.home_tab_center_x, position_info.home_tab_center_y), true);
        return position_info;
    end;

    handle_show_home_tab_flashing = function ()
        ui_utility.setproperty_by_id(navigation_tab.home_tab_ani_id, "Background", "NONE");
        ui_utility.setproperty_by_id(navigation_tab.home_tab_ani_id, "Background", "ONEIMAGE:client_mainframe\\tab\\home_flashing.tfc;Stretch;0;");
    end;

    handle_hide_home_tab_flashing = function()
        ui_utility.setproperty_by_id(navigation_tab.home_tab_ani_id, "Background", "NONE");
    end;

    move_tab = function (tab_id)
        local tab_info = navigation_tab.btn_info[tab_id];
        if nil ~= tab_info then
            tab_info.tab_offset_x = tonumber(tab_info.tab_offset_x) + 100;
        end
        local tab_btn_ptr = ui_utility.get_ctrl_ptr_by_id(tab_id);
        if nil ~= tab_btn_ptr then
            local tab_btn_rct = tab_btn_ptr:GetRectEx(FALSE,TRUE);
            tab_btn_rct.left = tab_btn_rct.left + 100;
            tab_btn_rct.right = tab_btn_rct.right + 100;
            tab_btn_ptr:MoveEx(tab_btn_rct,TRUE);
        end
    end;

    proc_ui_node = function (ui_info_path)
        if nil == navigation_tab.tab_ctn_ptr then
            return;
        end
        local xml = newParser();
        local xmlparser = xml:loadVfsFile(ui_info_path);
        if nil == xmlparser or nil == xmlparser.ui_info then
            return false;
        end
        local item_ctrl_id = navigation_tab.tab_ctn_ptr:AddOneItem();
        local ui_info = {};
        ui_info["plugin_name"] = xmlparser.ui_info["@name"];

        if nil ~= xmlparser.ui_info.nor_bkg then
            ui_info["nor_bkg"] = xmlparser.ui_info.nor_bkg["@name"];
        end
        if nil ~= xmlparser.ui_info.nor_hover_ani then
            ui_info["nor_hover_ani"] = xmlparser.ui_info.nor_hover_ani["@name"];
        end
        if nil ~= xmlparser.ui_info.slct_ani then
            ui_info["slct_ani"] = xmlparser.ui_info.slct_ani["@name"];
        end
        if nil ~= xmlparser.ui_info.un_slct_ani then
            ui_info["un_slct_ani"] = xmlparser.ui_info.un_slct_ani["@name"];
        end
        if nil ~= xmlparser.ui_info.nor_leave_ani then
            ui_info["nor_leave_ani"] = xmlparser.ui_info.nor_leave_ani["@name"];
        end
        if nil ~= xmlparser.ui_info.tab_offset_x then
            ui_info["tab_offset_x"] = tonumber(xmlparser.ui_info.tab_offset_x["@name"]);
        end
        if nil ~= xmlparser.ui_info.tab_offset_y then
            ui_info["tab_offset_y"] = tonumber(xmlparser.ui_info.tab_offset_y["@name"]);
        end
        if nil ~= xmlparser.ui_info.module_id then
            ui_info["module_id"] = tonumber(xmlparser.ui_info.module_id["@name"]);
            navigation_tab.module_id_2_tab_id[tonumber(ui_info.module_id)] = item_ctrl_id;
            if GAME_ID.GAEM_ID_HOME_PAGE == tonumber(ui_info.module_id) then
                navigation_tab.home_tab_id = item_ctrl_id;
            end
        end
        if nil ~= xmlparser.ui_info.menu_path then
            ui_info["menu_path"] = xmlparser.ui_info.menu_path["@name"];
        end
        if nil ~= xmlparser.ui_info.normal_en then
            ui_info["normal_en"] = xmlparser.ui_info.normal_en["@name"];
        end
        if nil ~= xmlparser.ui_info.hover_en then
            ui_info["hover_en"] = xmlparser.ui_info.hover_en["@name"];
        end
        if nil ~= xmlparser.ui_info.press_en then
            ui_info["press_en"] = xmlparser.ui_info.press_en["@name"];
        end

        navigation_tab.btn_info[item_ctrl_id] = ui_info;
        navigation_tab.set_btn_nor_status(item_ctrl_id);
        local inner_btn_id = ui_utility.get_ctrl_id_by_path(item_ctrl_id, "btnIcon");
        ui_utility.setproperty_by_id(inner_btn_id, "OnClick", "navigation_tab.on_click_tab");
    end;

    send_click_event = function (plugin_name)
        local event_id = lua_plugin_mgr.get_plugin_run_event("navigation_tab", plugin_name);
        if nil ~= event_id and "" ~= event_id then
            comm_center.send_broadcast(event_id, {});
        end
    end;
};